home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / misc / shokdial / shokdial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-07  |  47.1 KB  |  1,591 lines

  1. /*                   ShokDial                                   */
  2. /*   w00w00!                                                            */
  3. /*   This is (I have never seen one anyway, I apologize if I'm wrong)   */
  4. /*   the first war dialer that I've ever seen for unix. This will       */
  5. /*   compile on most/all unix operating systems.                        */
  6. /*                                    */
  7. /*            Shok (Matt Conover), shok@dataforce.net                  */
  8.  
  9.  
  10. #include <time.h>
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <ctype.h>
  15. #include <unistd.h>
  16. #include <string.h>
  17. #include <signal.h>
  18. #include <sys/types.h>
  19.  
  20. #include "colors.h"
  21.  
  22. #define ERROR -1
  23. #define LOGFILE "wardial.log" /* 
  24.                    * Used as the default logfile,       
  25.                                * unless you change this define      
  26.                                * or specify it as an option.
  27.                                * Type: shokdial -h for help.
  28.                    */ 
  29.                 
  30.  
  31. #define VERSION "v4.1"
  32. #define TIMEOUT 25     /* 
  33.             * YOU WANT TO CONFIGURE THIS!!!
  34.                         * This is how long it will wait until it
  35.                         * gives up (or connects, whichever comes first
  36.             */
  37.  
  38. /* You can do:
  39.  * ln -s /dev/cua1 /dev/modem
  40.  * or change this  to /dev/cua1 (or whatever your COM is)
  41.  * cua0 = COM1 cua1 = COM2
  42.  * (in linux)...in IRIX this would be /dev/ttymX I believe
  43.  */
  44.  
  45. #define MODEMPORT "/dev/modem"
  46.  
  47.  
  48. /*             Global variables                  */
  49. /*             ----------------                  */
  50. int  fd;                /* fd for modem                        */
  51. int  rand;            /* Use random scanning if this is set         */
  52. int  send;              /* Do we send a string to the carrier?        */
  53. int  daemon;            /* Do we fork into the background?            */
  54. int  listen;            /* Do we check a response from the carrier?   */
  55. int  useStdin;          /* Do we read numbers from stdin?             */
  56. int  numbytes;          /* To verify that all the bytes were written  */
  57.  
  58. int  First3Digits;      /* Such as "555" of 555-XXXX                   */
  59.                         /* However, this also serves as the area code */
  60.                         /* for a long distance number                 */
  61.  
  62. int  First3Digits1;     /* This allows multiple ranges such as        */
  63.                         /* 555-XXXX through 556-XXXX                  */
  64.  
  65. int  Last3Digits;       /* Used as XXX-555-XXXX                       */
  66. int  Last3Digits1;      /* Same purpose as First3Digits1              */
  67. int  ScanMin;           /* Number to scan from....like 0000 and up    */
  68. int  ScanMin1;          /* Where to hold ScanMin the whole time       */
  69. int  ScanMax;           /* Stop scanning when this number is reached  */
  70. int  response;          /* Used to test if response timed out         */
  71. char *LogFile;          /* Where to log connections               */
  72. char buf[2048];            /* Buffer for strings returned by modem       */
  73. char pnum[512];         /* This is the phone number from config file  */
  74. char LocalOrLong;       /* Dialing long distance of local           */
  75. char sendstring[512];   /* Send to string to carrier (if send is set) */
  76.  
  77. char *ProgName;      
  78.  
  79. int  noshow;            /* Don't display opening port when reopening  */
  80. int  conf;              /* Dial using config file                     */
  81. int  noOK;              /* Used with hanging up and checking "OK"     */
  82.       
  83. volatile int sig;           /* Set after signal received and finished */
  84. volatile int connected = 0; /* Set to 1 when connected.               */
  85.  
  86. /* Some statistics.  */
  87. int busy       = 0;
  88. int connect    = 0;
  89. int noresponse = 0;
  90.  
  91. /*             Function prototypes                   */ 
  92. /*             -------------------                   */
  93. void usage();              /*  Help/usage                  */
  94. void version();               /*  Display version                     */
  95. void intro();                   /*  An introduction              */
  96. void daemonize_me();           /*  Fork into the background            */
  97. void get_scanrange();         /*  Get the scanning range              */
  98. void open_port();             /*  Open modem port for dialing          */
  99. void init_modem();            /*  Initialize the modem              */
  100. void dial_number();          /*  Dial the number              */
  101. void inputdial();             /*  Read numbers from stdin             */
  102. void confdial(char *confile); /*  For reading/dialing from conf file  */
  103. void hangup();                /*  Hang up modem.                      */
  104. void menu(int signum);        /*  Called when an abort is received.   */
  105. void sighandler(int signum);  /*  Used when signals are received      */
  106. void sighandler1(int signum); /*  Ditto                               */
  107. void stopnow(int signum);     /*  Called from sig handler for an un-  */
  108.                               /*  conditional exit.                   */
  109.  
  110. /* Function prototypes in other source files: */
  111. /* ------------------------------------------ */
  112. /* Check read/write/opens for errors  */
  113. void check_for_error(char *LogFile, int fd, int num, char *s);
  114.  
  115. /* Check for "OK" from modem in reads. */
  116. int checkok(char *LogFile, int fd, char *buf, char *s);
  117.  
  118. /* Check if the phone num was valid. */ 
  119. void local_validnum(int digits);
  120. void long_validnum(int firstdigits, int lastdigits);
  121.  
  122. /* Check to make sure they didn't pass conflicting options. */
  123. void checkoptions();
  124.  
  125. /* Other miscellaneous prototypes included to avoid. */
  126. int  clr();
  127. void strip();
  128.  
  129. int main(int argc, char **argv)
  130. {
  131.   int  opt;
  132.   char *confile;
  133.  
  134.   clr(); /* Clear the screen. */
  135.  
  136.   /* Do some stuff with the arguments                      */
  137.   /* ----------------------------------------------------- */
  138.  
  139.   ProgName = argv[0];
  140.  
  141.   if (argc > 1) {
  142.       while ((opt = getopt (argc, argv, "SsrdvhL:lc:")) != ERROR)
  143.         switch(opt)
  144.        {
  145.         case 'S':
  146.             useStdin = 1;
  147.             break;
  148.  
  149.                 case 's':
  150.             send = 1;
  151.             break;
  152.  
  153.         case 'r':
  154.              rand = 1;
  155.                  break;
  156.             
  157.                 case 'd':
  158.             daemon = 1;
  159.                     break;
  160.  
  161.         case 'v': 
  162.             version();
  163.  
  164.         case 'h':
  165.             usage();
  166.  
  167.         case 'L':
  168.             LogFile = optarg;
  169.             break;
  170.             
  171.                 case 'l':
  172.             listen = 1;
  173.             break;
  174.  
  175.         case 'c':
  176.             conf = 1;
  177.             confile = optarg;
  178.             break;
  179.  
  180.         case '?':
  181.             putchar('\n');
  182.             usage();
  183.  
  184.         default:
  185.             usage();
  186.       }
  187.   }
  188.  
  189.   /* Check to make sure they didn't pass conflicting options. */  
  190.   checkoptions(); /* exit()'s if there is an error */
  191.  
  192.   if (conf != 1 && useStdin != 1) 
  193.      printf("\"%s-r%s\" (%srandom scanning%s) option not given, using %ssequential scanning%s instead.\n", 
  194.          PINK, NORMAL, BOLDWHITE, NORMAL, BOLDRED, NORMAL);
  195.  
  196.   if (LogFile == NULL) {
  197.      LogFile = LOGFILE;
  198.      printf("Using \"%s%s%s\" as log file.\n", BOLDGREEN, LogFile, NORMAL);
  199.   }
  200.  
  201.   printf("\nHit any key to continue...");
  202.   getchar();
  203.  
  204. /* ----------------------------------------------------- */
  205.  
  206.   clr(); /* Clear the screen. */
  207.   intro();
  208.  
  209.   clr(); /* Clear the screen. */
  210.   if (conf != 1 && useStdin != 1) get_scanrange();
  211.  
  212.  
  213.   /* We don't want to handle any signals until here */
  214.   signal(SIGINT,  menu);
  215.   signal(SIGTERM, menu);
  216.   signal(SIGHUP,  SIG_IGN);
  217.   signal(SIGALRM, sighandler1);
  218.  
  219.   if (daemon == 1) 
  220.      daemonize_me();      /* Run the program in the background           */
  221.  
  222.   open_port();            /* Open MODEMPORT (by default /dev/cua1)       */
  223.   init_modem();           /* Initialize modem (such as sending ATZ)      */
  224.  
  225.   if (send == 1) {
  226.      printf("Enter string to send to carrier (when connected): ");
  227.      scanf("%512s", sendstring);
  228.   }
  229.  
  230.   /* What type of dialing are we using? */
  231.   if (conf == 1)
  232.      confdial(confile);   /* Read numbers to dial from a config file     */
  233.  
  234.   else if (useStdin == 1)
  235.      inputdial();         /* Read numbers from stdin                 */
  236.  
  237.   else dial_number();     /* Do the scanning (used by default, instead   */
  238.                           /* of confdial(), inputdial(), etc.)           */
  239.  
  240.   /* ---------------------------------- */
  241.  
  242.   hangup();               /* Hang up the modem                     */
  243.   close(fd);              /* Close the open file descriptor of the modem */
  244.  
  245.   return 0;
  246. }
  247.  
  248. /* -------------------------------------------------- */
  249.  
  250. void version()
  251. {
  252.   printf("This is %sS%sh%so%sk%sD%si%sa%sl %s%s%s...please keep notice of this.\n", 
  253.       BOLDCYAN, BOLDGREEN, BOLDBLUE, BOLDPINK, YELLOW, BOLDWHITE,
  254.           BOLDRED, PINK, BOLDBLUE, VERSION, NORMAL);
  255.  
  256.   printf("in case this program under goes some new features, fixes, etc.\n\n");
  257.  
  258.   printf("\t\t\t%s Shok %s\n\t\t    (%sMatt Conover%s)\n\n", 
  259.       BOLDBLUE, NORMAL, BOLDWHITE, NORMAL);
  260.  
  261.   printf("%sEmail%s: %sshok@dataforce.net%s\n",
  262.          BOLDWHITE, NORMAL, PINK, NORMAL);
  263.  
  264.   printf("%sWWW%s: %shttp://www.w00w00.org/%s\n",
  265.          BOLDWHITE, NORMAL, PINK, NORMAL);
  266.  
  267.  
  268.   exit(0);
  269. }
  270.  
  271. /* -------------------------------------------------- */
  272.  
  273. void usage()
  274. {
  275.   printf("Usage: %s%s %s[-rhvdSsl]%s -c [config file]%s -L [logfile]%s\n\n", PINK, ProgName, BOLDWHITE, BOLDCYAN, BOLDGREEN, NORMAL);
  276.   printf("Options:\n");
  277.   printf("%s-r%s for %srandom%s (as opposed to %ssequential%s) scanning\n", BOLDCYAN, NORMAL, PINK, NORMAL, YELLOW, NORMAL);
  278.   printf("%s-h%s for %shelp%s....what you're seeing now\n", PINK, NORMAL, BOLDRED, NORMAL);
  279.   printf("%s-v%s for the %sversion%s...because this will probably undergo changes\n", BOLDGREEN, NORMAL, BOLDCYAN, NORMAL);
  280.   printf("%s-d%s to run in the %sbackground%s.\n", BLUE, NORMAL, BOLDGREEN, NORMAL);
  281.   printf("%s-S%s to read numbers from %sstdin%s\n", PINK, NORMAL, BOLDRED, NORMAL);
  282.   printf("%s-l%s to listen for a %sresponse%s from the carrier\n", BOLDCYAN, NORMAL, PINK, NORMAL);
  283.   printf("%s-s%s to send a %sstring%s to the carrier\n", BOLDGREEN, NORMAL, BOLDCYAN, NORMAL);
  284.   printf("%s-c%s to read phone numbers from a %sconfig file%s.\n", YELLOW, NORMAL, BOLDCYAN, NORMAL);
  285.   printf("%s-L%s to specify the %slogfile%s.\n", BOLDRED, NORMAL, PINK, NORMAL);
  286.  
  287.   putchar('\n');
  288.  
  289.   printf("The %slogfile%s is by default %s%s%s if not specified.\n", BOLDCYAN, NORMAL, BOLDGREEN, LOGFILE, NORMAL);
  290.   printf("The %sconfig file%s is only specified if %s-c%s option is used.\n", PINK, NORMAL, BOLDCYAN, NORMAL);
  291.  
  292.   putchar('\n');
  293.   exit(1);
  294. }
  295.  
  296. /* -------------------------------------------------- */
  297.  
  298. void intro()
  299. {
  300.   printf("\t\t%sS%sh%so%sk%sd%si%sa%sl%s %s%s %sf%so%sr %sU%sN%si%sX%s\n", 
  301.       BLINKCYAN, BOLDGREEN, BOLDBLUE, BOLDPINK, YELLOW, BOLDWHITE, 
  302.           BOLDRED,   PINK, BOLDBLUE, VERSION, NORMAL, PINK, BOLDCYAN,
  303.           BOLDGREEN, BOLDPINK, BOLDGREEN, BOLDWHITE, BOLDBLUE, NORMAL);
  304.  
  305.   printf("\t\t----------------------\n");
  306.   printf("\nWell what you do here, is enter 0000 for the range to begin\n");
  307.   printf("scanning and 9999 to end scanning if you want to scan all the\n");
  308.   printf("possible ranges, but you can put 4444 for the nmber to start\n");
  309.   printf("and 5555 for the number to begin to scan XXX-[4444-5555] for\n"); 
  310.   printf("local numbers and it would be 1-XXX-XXX-[4444-5555] for long\n");
  311.   printf("distance.\n");
  312.   printf("\nAlso, you can use random scanning (as opposed to sequential\n");
  313.   printf("scanning) by specifying the \"%s-r%s\" option...type:\n", 
  314.       PINK, NORMAL);
  315.  
  316.   printf("%s%s%s -h %sfor %shelp%s.\n\n", 
  317.       BOLDRED, ProgName, BOLDRED, NORMAL, BOLDCYAN, NORMAL);
  318.  
  319.   printf("Anyway, enjoy!\n\n");
  320.  
  321.   printf("\t\t\t%s Shok %s\n\t\t    (%sMatt Conover%s)\n\n", 
  322.       BOLDBLUE, NORMAL, BOLDWHITE, NORMAL);
  323.  
  324.   printf("%sEmail%s: %sshok@dataforce.net%s\n",
  325.          BOLDWHITE, NORMAL, PINK, NORMAL);
  326.  
  327.   printf("%sWWW%s: %shttp://www.w00w00.org/%s\n",
  328.          BOLDWHITE, NORMAL, PINK, NORMAL);
  329.  
  330.  
  331.   printf("Hit enter to continue...\n");
  332.   getchar();
  333. }
  334.  
  335. /* -------------------------------------------------- */
  336.  
  337. void daemonize_me() 
  338. {
  339.   pid_t pid;
  340.  
  341.   if ((pid = fork()) == ERROR) {
  342.      perror("fork");
  343.      exit(ERROR);
  344.   }
  345.  
  346.   if (pid != 0) 
  347.      exit(0);
  348. }
  349.  
  350. /* -------------------------------------------------- */
  351.  
  352. void get_scanrange()
  353. {
  354.  
  355.   /* Get location of numbers: local numbers or long distance numbers */
  356.   LorD:
  357.     printf("Scanning..\n(%sL%s)ocal, Long (%sD%s)istance: ", 
  358.           PINK, NORMAL, PINK, NORMAL);
  359.  
  360.   while(1) {
  361.      LocalOrLong = getchar();
  362.  
  363.      if (!isprint(LocalOrLong)) continue;
  364.      if ((toupper(LocalOrLong) != 'L') && (toupper(LocalOrLong) != 'D')) {
  365.         printf("%sInvalid%s option '%s%c%s'. Enter '%sL%s' or '%sD%s'.\n\n",
  366.                BOLDRED, NORMAL, BOLDCYAN, LocalOrLong, NORMAL, YELLOW,
  367.                NORMAL, YELLOW, NORMAL);
  368.         goto LorD; /* Reprint message. */ 
  369.     } else break;
  370.   }
  371.  
  372.   if (toupper(LocalOrLong) == 'L') { /* Use local phone numbers */
  373.      if (rand != 1) { /* Using sequential scanning */
  374.  
  375.         printf("Enter number to begin scan on (555-1111): ");
  376.         scanf("%3d%*c%4d", &First3Digits, &ScanMin);
  377.  
  378.         local_validnum(First3Digits); /* Make sure the first 3 digits */
  379.                                       /* were a valid number.         */
  380.  
  381.         ScanMin1 = ScanMin; /* ScanMin changes, so we need a second   */
  382.                        /* variable to store the original number. */
  383.  
  384.      } else { /* Using random scanning */
  385.  
  386.         printf("Enter the first 3 digits (555 for random scanning of 555-XXXX): ");
  387.         scanf("%3d", &First3Digits);
  388.       
  389.     local_validnum(First3Digits); /* Make sure the first 3 digits */
  390.                                       /* were a valid number.         */
  391.  
  392.         ScanMin1 = ScanMin; /* ScanMin changes, so we need a second   */
  393.                      /* variable to store the original number. */
  394.      }
  395.  
  396.      /* Make sure the last 4 digits were valid */
  397.      if ((ScanMin < 0) || (ScanMin > 9999)) {
  398.     printf("\"%s%d%s\" is invalid.\nScanning range must be %s0000-9999%s\n", 
  399.         BOLDCYAN, ScanMin, NORMAL, PINK, NORMAL);
  400.     exit(ERROR);
  401.      }
  402.  
  403.      if (rand != 1) { /* Using sequential scanning */
  404.  
  405.         printf("Enter number to end scanning on (555-9999): ");
  406.         scanf("%3d%*c%4d", &First3Digits1, &ScanMax);
  407.  
  408.         local_validnum(First3Digits1); /* Make sure the first 3 digits */
  409.                                        /* were a valid number.         */ 
  410.         putchar('\n');
  411.   
  412.         if ((ScanMax < ScanMin) || (ScanMax < 0) || (ScanMax > 9999)) {
  413.          printf("\"%s%d%s\" is invalid.\n Scanning range must be %s0000-9999%s, and the %smaximum%s range must be %sgreater%s\nthan or equal to the %sminimum%s number.\n", 
  414.            BOLDCYAN, ScanMax, NORMAL, BOLDWHITE, NORMAL, PINK,
  415.            NORMAL, BOLDWHITE, NORMAL, PINK, NORMAL);
  416.  
  417.         exit(ERROR);
  418.         }
  419.      } else 
  420.     putchar('\n');
  421.  
  422.  
  423.   /* -------------------- */
  424.  
  425.   } else if (toupper(LocalOrLong) == 'D') { /* Use long distance numbers */
  426.  
  427.       if (rand != 1) { /* Use sequential scanning */
  428.       printf("Enter number to start scanning (555-555-1111): ");
  429.        scanf("%3d%*c%3d%*c%4d", &First3Digits, &Last3Digits, &ScanMin);
  430.  
  431.          /* Check if area code and first 3 digits of the phone num are */
  432.          /* valid.                                */
  433.      long_validnum(First3Digits, Last3Digits);
  434.  
  435.          ScanMin1 = ScanMin; /* ScanMin changes, so we need a second   */
  436.                        /* variable to store the original number. */
  437.  
  438.         /* ... */
  439.  
  440.       } else { /* Using random scanning */
  441.  
  442.      printf("Enter the area code and prefix digits\n(555-555 for random scanning of 555-555-XXXX): ");
  443.          scanf("%3d%*c%3d", &First3Digits, &Last3Digits);
  444.  
  445.          /* Check if area code and first 3 digits of the phone num are */
  446.          /* valid.                               */
  447.        long_validnum(First3Digits, Last3Digits);
  448.  
  449.          ScanMin1 = ScanMin; /* ScanMin changes, so we need a second   */
  450.                       /* variable to store the original number. */
  451.       }
  452.  
  453.       /* Make sure the last 4 digits were valid */
  454.       if ((ScanMin < 0) || (ScanMin > 9999)) {
  455.       printf("\"%s%d%s\" is invalid.\nScanning range must be %s0000-9999%s\n", 
  456.           BOLDCYAN, ScanMin, NORMAL, PINK, NORMAL);
  457.      exit(ERROR);
  458.       }
  459.  
  460.       if (rand != 1) { /* Using sequential scanning */
  461.  
  462.      printf("Enter number to end scanning (555-555-9999): ");
  463.        scanf("%3d%*c%3d%*c%4d", &First3Digits1, &Last3Digits1, &ScanMax);
  464.  
  465.        putchar('\n');
  466.   
  467.          /* Check if area code and first 3 digits of the phone num are */
  468.          /* valid.                              */
  469.        long_validnum(First3Digits1, Last3Digits1);
  470.  
  471.          if ((ScanMax < ScanMin) || (ScanMax < 0) || (ScanMax > 9999)) {
  472.            printf("\"%s%d%s\" is invalid.\n Scanning range must be %s0000-9999%s, and the %smaximum%s range must be %sgreater%s\nthan or equal to the %sminimum%s number.\n", 
  473.             BOLDCYAN, ScanMax, NORMAL, BOLDWHITE, NORMAL, PINK,
  474.             NORMAL, BOLDWHITE, NORMAL, PINK, NORMAL);
  475.  
  476.         exit(ERROR);
  477.        }
  478.       } else 
  479.          putchar('\n');
  480.  
  481.   } else {
  482.        printf("You must specify \"%sL%s\" for %slocal%s or \"%sD%s\" for %slong distance%s\n", 
  483.             PINK, NORMAL, BOLDCYAN, NORMAL, PINK, NORMAL, BOLDCYAN, NORMAL);
  484.        exit(ERROR);
  485.   }
  486.  
  487. }
  488.  
  489. /* -------------------------------------------------- */
  490.  
  491. void open_port()
  492. {
  493.   if (noshow != 1) printf("Opening modem for dialing...\n");
  494.  
  495.   fd = open(MODEMPORT, O_RDWR | O_NOCTTY);
  496.  
  497.   if (fd == ERROR) {
  498.      perror("open");
  499.      exit(ERROR);
  500.   }
  501.   
  502.   noshow = 1; /* We use this function for reopening as well */
  503. }
  504.  
  505. /* -------------------------------------------------- */
  506.  
  507. void init_modem()
  508. {
  509.   FILE *logfile;
  510.  
  511.   if ((logfile = fopen(LogFile, "a")) == NULL) {
  512.      perror("fopen");
  513.      close(fd);
  514.      exit(ERROR);
  515.   }
  516.  
  517.   printf("Initializing modem (port %s%s%s)....\n", PINK, MODEMPORT, NORMAL);
  518.  
  519.   /* Hang up modem if it's already on */
  520.   
  521.   hangup();
  522.  
  523.   numbytes = write(fd, "+++\r", 4);
  524.   check_for_error(LogFile, fd, numbytes, "write");
  525.   usleep(1000000);
  526.  
  527.   numbytes = write(fd, "ATZ\r", 4);
  528.   check_for_error(LogFile, fd, numbytes, "write");
  529.   usleep(2000000); /* Use this because we're using SIGALRM which   */
  530.            /* is what sleep() uses.               */
  531.  
  532.   memset(buf, 0, sizeof(buf));
  533.   numbytes = read(fd, buf, sizeof(buf));
  534.   check_for_error(LogFile, fd, numbytes, "read");
  535.  
  536.   noOK = checkok(LogFile, fd, buf, "initializing modem");
  537.   
  538.   if (noOK == 1) {
  539.      fclose(logfile);
  540.      close(fd);
  541.      exit(ERROR);
  542.   }
  543.  
  544.   memset(buf, 0, sizeof(buf));
  545.  
  546.   fclose(logfile);
  547. }
  548.  
  549. /* -------------------------------------------------- */
  550.  
  551. void dial_number()
  552. {
  553.   time_t tm;           /* Where we our calendar time is stored        */
  554.   FILE   *logfile;     /* for the log file                        */
  555.   char   date[32];     /* Contain time scanning started/stopped         */
  556.   char   phonenum[20]; /* If local: phonenum = First3Digits + ScanMin   */
  557.                        /* If long distance: phonenum =                  */
  558.                        /* First3Digits + Last3Digits + ScanMin          */
  559.     
  560.   if ((logfile = fopen(LogFile, "a")) == NULL) {
  561.      perror("fopen");
  562.      exit(ERROR);
  563.   }
  564.  
  565.   fprintf(logfile, "\n----------------------\n\n");
  566.   fflush(logfile);
  567.  
  568.   memset(buf, 0, sizeof(date));
  569.   memset(buf, 0, sizeof(buf));
  570.  
  571.   tm = time(NULL);
  572.   sprintf(date, "%s", ctime(&tm));
  573.   fprintf(logfile, "Started scanning at/on: %s", date);
  574.  
  575.   fflush(logfile);
  576.   memset(date, 0, sizeof(date));
  577.  
  578.   if (daemon == 1) putchar('\n'); /* Just to make it look nicer */
  579.  
  580.   printf("Using a %s%d%s second connection %stimeout%s.\n", 
  581.       BOLDCYAN, TIMEOUT, NORMAL, BOLDWHITE, NORMAL);
  582.  
  583.   
  584.   if (toupper(LocalOrLong) == 'L') {  /* Local call */
  585.  
  586.      fprintf(logfile, "Scanning local numbers...\n");
  587.      fprintf(logfile, "Using a %d second connection timeout.\n", TIMEOUT);
  588.      fprintf(logfile, "Starting scanning with %d-%.4d\n\n", 
  589.          First3Digits, ScanMin);
  590.  
  591.      fflush(logfile);
  592.  
  593.      while (1) {
  594.         if (rand == 1) ScanMin = (random() % 8889) + 11;
  595.  
  596.           printf("Dialing %s%d-%.4d%s...\n", 
  597.             PINK, First3Digits, ScanMin, NORMAL);
  598.  
  599.     memset(phonenum, 0, sizeof(phonenum));
  600.     sprintf(phonenum, "ATDT%d%.4d\r", First3Digits, ScanMin);
  601.  
  602.            numbytes = write(fd, phonenum, strlen(phonenum));
  603.     check_for_error(LogFile, fd, numbytes, "write");
  604.  
  605.     memset(buf, 0, sizeof(buf));
  606.  
  607.         alarm(TIMEOUT); /* How long to wait for timeout   */
  608.  
  609.     sig = 0;
  610.         connected = 1; /*  
  611.             * Easier to set it to 1 and then set it
  612.             * to 0 if it's not than vice versa.
  613.             */
  614.     do {
  615.            numbytes = read(fd, buf, 511);
  616.        if (sig == 1) break;
  617.             
  618.     } while ((strstr(buf, "CONNECT")) == NULL);
  619.  
  620.     alarm(0); /* Turn alarm off if we haven't already. */
  621.  
  622.     if (connected == 0) noresponse++;
  623.     else if ((strstr(buf, "BUSY")) != NULL) busy++;
  624.  
  625.            /* Compare the string with "CONNECT" */
  626.         if (connected == 1) { /* Sighandler sets this to 0 when  */
  627.                   /* it's called...meaning time out. */
  628. #ifdef BEEP
  629.            putchar('\a');
  630. #endif
  631.  
  632.        connect++;
  633.  
  634.            fprintf(logfile, "*** CONNECT *** to %d-%.4d\n",  
  635.            First3Digits, ScanMin);
  636.  
  637.         printf("%s*** %sCONNECT %s%s*** %s to %s%d-%.4d%s\n", 
  638.              BOLDWHITE, BOLDCYAN, NORMAL, BOLDWHITE, NORMAL, 
  639.             PINK, First3Digits, ScanMin, NORMAL);
  640.  
  641.            /* Send a string to the carrier and check for response */
  642.            if (send && listen) { /* send poke string and listen for reply */
  643.  
  644.               if (write(fd, sendstring, sizeof(sendstring)) == ERROR) {
  645.          perror("write");
  646.  
  647.          close(fd);
  648.                  fclose(logfile);
  649.          exit(ERROR);
  650.               }
  651.  
  652.               response = 1; /* Sighandler will set this to 0 when it */
  653.                             /* times out                             */
  654.  
  655.               printf("response from carrier (after sending string): ");
  656.               fprintf(logfile, "response from carrier (after sending string): ");
  657.               fflush(stdout), fflush(logfile);
  658.  
  659.           if (read(fd, buf, sizeof(buf)) == ERROR) {
  660.          perror("read");
  661.                  printf("continuing anyway...\n");
  662.           }
  663.  
  664.               if (response == 1) {
  665.                  printf("%s\n", buf);
  666.                  fprintf(logfile, "%s\n", buf);
  667.               } else {
  668.                  printf("timed out while waiting for response\n");
  669.                  fprintf(logfile, "timed out while waiting for response\n");
  670.               }
  671.            } else { /* listen = 1, send = 0 */
  672.  
  673.               response = 1; /* The sighandler will set this to 0 if it */
  674.                             /* times out                               */
  675.  
  676.               printf("response from carrier: ");
  677.               fprintf(logfile, "response from carrier: ");
  678.  
  679.           if (read(fd, buf, sizeof(buf)) == ERROR) {
  680.          perror("read");
  681.                  printf("continuing anyway...\n");
  682.           }
  683.  
  684.               if (response == 1) {
  685.                  printf("%s\n", buf);
  686.                  fprintf(logfile, "%s\n", buf);
  687.               } else {
  688.                  printf("timed out while waiting for response\n");
  689.                  fprintf(logfile, "timed out while waiting for response\n");
  690.               }
  691.            }
  692.         }
  693.  
  694.         memset(buf, 0, sizeof(buf)); 
  695.  
  696.     hangup();
  697.  
  698.     if (rand != 1) {
  699.        /* Increase ScanMin so it scans for the next number */
  700.        ScanMin++;
  701.             
  702.            if (ScanMin > ScanMax) {
  703.            /* If they are different...then they are scanning */
  704.               /* something like: 555-XXXX through 556-XXXX.     */
  705.    
  706.               /* So now we reset everything.       */
  707.  
  708.               /* 
  709.                * If you did: 755-XXXX through 757-XXXX, we need to 
  710.                * increase the 755 and repeat until they are the same. 
  711.                */
  712.  
  713.                if (First3Digits !=  First3Digits1) {
  714.  
  715.                  First3Digits++; 
  716.               ScanMin = ScanMin1; /* Restored ScanMin to its */
  717.                                       /* original value.         */
  718.                 continue;
  719.            }
  720.  
  721.            memset(buf, 0, sizeof(buf));
  722.  
  723. #ifdef BEEP
  724.                putchar('\a');
  725. #endif
  726.  
  727.            fprintf(logfile, "\nFinished scanning %d-%.4d through %d-%d.\n", 
  728.                   First3Digits, ScanMin1, First3Digits, ScanMax);
  729.                 
  730.            memset(date, 0, sizeof(date));
  731.            tm = time(NULL);
  732.            sprintf(date, "%s", ctime(&tm));
  733.  
  734.            fprintf(logfile, "Finished at/on: %s", date);
  735.            fflush(logfile);                
  736.  
  737.            printf("Finished scanning %s%d-%.4d %sthrough %s%d-%.4d%s.\n", 
  738.                BOLDCYAN, First3Digits, ScanMin1, NORMAL, 
  739.                BOLDCYAN, First3Digits, ScanMax,  NORMAL);
  740.  
  741.                 /* Print statistics. */
  742.               printf("%sResults%s:\n", BOLDRED, NORMAL);
  743.  
  744.            printf("\t# of %ssuccessful connects%s: %s%d%s\n", 
  745.                    BOLDCYAN, NORMAL, PINK, connect, NORMAL);
  746.  
  747.            printf("\t# of lines %sbusy%s: %s%d%s\n", 
  748.                    YELLOW, NORMAL, PINK, busy, NORMAL);
  749.  
  750.            printf("\t# of %sno responses (timed out)%s: %s%d%s\n", 
  751.                BOLDGREEN, NORMAL, PINK, noresponse, NORMAL);
  752.  
  753.  
  754.              printf("Thanks for using %sS%sh%so%sk%sD%si%sa%sl %s%s%s.\n", 
  755.                 BLINKCYAN, BOLDGREEN, BOLDBLUE, BOLDPINK, YELLOW, 
  756.                        BOLDWHITE, BOLDRED, PINK, BOLDBLUE, VERSION, NORMAL);
  757.  
  758.            return;
  759.            } 
  760.         }
  761.  
  762.     memset(phonenum, 0, sizeof(phonenum));
  763.  
  764.     }
  765.   } else { /* (Long Distance call) */ 
  766.  
  767.         memset(buf, 0, sizeof(buf));
  768.  
  769.     fprintf(logfile, "Scanning long distance numbers...\n");
  770.         fprintf(logfile, "Using a %d second connection timeout.\n", TIMEOUT);
  771.     fprintf(logfile, "Started scanning with 1-%.3d-%.3d-%.4d\n\n", 
  772.         First3Digits, Last3Digits, ScanMin);
  773.  
  774.         fflush(logfile);
  775.  
  776.     while(1) {
  777.          if (rand == 1) ScanMin = (random() % 8889) + 1111;
  778.  
  779.        printf("Dialing %s1-%.3d-%.3d-%.4d%s...\n", 
  780.              PINK, First3Digits, Last3Digits, ScanMin, NORMAL);
  781.  
  782.         memset(phonenum, 0, sizeof(phonenum));
  783.        sprintf(phonenum, "ATDT1%.3d%.3d%.4d\r", 
  784.            First3Digits, Last3Digits, ScanMin);
  785.  
  786.            numbytes = write(fd, phonenum, strlen(phonenum));
  787.            check_for_error(LogFile, fd, numbytes, "write");
  788.                
  789.            memset(buf, 0, sizeof(buf));
  790.  
  791.            alarm(TIMEOUT); /* How long to wait for timeout. */
  792.  
  793.            sig = 0;
  794.            connected = 1; /* 
  795.                            * Easier to say it's connected and then
  796.                            * set it to 0 if it's not than vice versa.
  797.                            */
  798.            do {
  799.               numbytes = read(fd, buf, 511);
  800.           if (sig == 1) break;
  801.            } while ((strstr(buf, "CONNECT")) == NULL); 
  802.     
  803.         alarm(0);
  804.  
  805.        if (connected == 0) noresponse++;
  806.        else if ((strstr(buf, "BUSY")) != NULL) busy++;
  807.  
  808.            if (connected == 1) { /* The sighandler sets this to 0 when */
  809.                  /* it gets called.                    */
  810.  
  811. #ifdef BEEP
  812.               putchar('\a');
  813. #endif
  814.  
  815.                connect++;
  816.  
  817.            fprintf(logfile, "*** CONNECT *** to 1-%.3d-%.3d-%.4d\n", 
  818.               First3Digits, Last3Digits, ScanMin);
  819.           fflush(logfile);
  820.  
  821.           printf("%s*** %sCONNECT %s%s*** %sto %s1-%.3d-%.3d-%.4d%s\n",
  822.               BOLDWHITE, BOLDCYAN, NORMAL, BOLDWHITE, NORMAL, PINK,
  823.               First3Digits, Last3Digits, ScanMin, NORMAL); 
  824.  
  825.               /* Send a string to the carrier and check for response */
  826.               if (send && listen) { /* send poke string and listen for reply */
  827.  
  828.                  if (write(fd, sendstring, sizeof(sendstring)) == ERROR) {
  829.             perror("write");
  830.  
  831.             close(fd);
  832.                     fclose(logfile);
  833.             exit(ERROR);
  834.                  }
  835.  
  836.                  response = 1; /* The sighandler sets this to 1 if it */
  837.                                /* times out                           */
  838.  
  839.                  printf("response from carrier (after sending string): ");
  840.                  fprintf(logfile, "response from carrier (after sending string): ");
  841.                  fflush(stdout), fflush(logfile);
  842.  
  843.                if (read(fd, buf, sizeof(buf)) == ERROR) {
  844.             perror("read");
  845.                     printf("continuing anyway...\n");
  846.              }
  847.  
  848.                  if (response == 1) {
  849.                     printf("%s\n", buf);
  850.                     fprintf(logfile, "%s\n", buf);
  851.                  } else {
  852.                     printf("timed out while waiting for response\n");
  853.                     fprintf(logfile, "timed out while waiting for response\n");
  854.          }
  855.  
  856.               } else { /* listen = 1, send = 0 */
  857.  
  858.                  response = 1; /* The sighandler sets this to 1 if it */
  859.                                /* times out.                          */
  860.  
  861.                  printf("response from carrier: ");
  862.          fprintf(logfile, "response from carrier: ");
  863.          fflush(stdout), fflush(logfile);
  864.  
  865.              if (read(fd, buf, sizeof(buf)) == ERROR) {
  866.             perror("read");
  867.                     printf("continuing anyway...\n");
  868.              }
  869.  
  870.                  if (response == 1) { 
  871.                     printf("%s\n", buf);
  872.                     fprintf(logfile, "%s\n", buf);
  873.                  } else {
  874.                     printf("timed out while waiting for response\n");
  875.                     fprintf(logfile, "timed out while waiting for response\n");
  876.                  }
  877.               }
  878.            }
  879.  
  880.            memset(buf, 0, sizeof(buf));
  881.  
  882.        hangup(); 
  883.  
  884.            if (rand != 1) {
  885.  
  886.               /* Increase ScanMin so it scans for the next number */
  887.                 ScanMin++;
  888.                         
  889.              if (ScanMin > ScanMax) {
  890.  
  891.         /* If they are different...then they are scanning */
  892.                 /* something like: 555-XXXX through 556-XXXX.     */
  893.   
  894.                 /* So now we reset everything.       */
  895.  
  896.                 /* 
  897.                  * If you did: 555-755-XXXX through 
  898.                  * 555-757-XXXX, we need to increase 
  899.                  * the 755 and repeat until they are the
  900.                  * same. 
  901.                  */
  902.  
  903.                  if ((First3Digits    != First3Digits1) 
  904.                       || (Last3Digits != Last3Digits1)) {
  905.  
  906.             if (First3Digits != First3Digits1) First3Digits++;
  907.             if (Last3Digits  != Last3Digits1)  Last3Digits++;
  908.  
  909.             ScanMin = ScanMin1; /* Restore to its original value */
  910.             continue;
  911.          }
  912.  
  913.          memset(buf, 0, sizeof(buf));
  914.  
  915. #ifdef BEEP
  916.                  putchar('\a');
  917. #endif 
  918.  
  919.          fprintf(logfile, "\nFinished scanning 1-%.3d-%.3d-%.4d through 1-%.3d-%.3d-%.4d.\n", 
  920.               First3Digits, Last3Digits, ScanMin1, First3Digits, 
  921.                          Last3Digits, ScanMax);
  922.                 
  923.           memset(date, 0, sizeof(date));
  924.          tm = time(NULL);
  925.          sprintf(date, "%s", ctime(&tm));
  926.             
  927.          fprintf(logfile, "Finished at/on: %s", date); 
  928.          fflush(logfile);
  929.  
  930.          printf("Finished scanning %s1-%.3d-%.3d-%.4d%s through %s1-%.3d-%.3d-%.4d%s", 
  931.               BOLDCYAN, First3Digits, Last3Digits, ScanMin1, 
  932.              NORMAL, BOLDCYAN, First3Digits, Last3Digits, 
  933.              ScanMax, NORMAL);
  934.  
  935.          /* Print statistics. */
  936.          printf("%sResults%s:\n", BOLDRED, NORMAL);
  937.  
  938.              printf("\t# of %ssuccessful connects%s: %s%d%s\n", 
  939.                  BOLDCYAN, NORMAL, PINK, connect, NORMAL);
  940.  
  941.            printf("\t# of %sbusy (timed out)%s: %s%d%s\n", 
  942.                  YELLOW, NORMAL, PINK, busy, NORMAL);
  943.  
  944.            printf("\t# of %sno responses (timed out)%s: %s%d%s\n", 
  945.                  BOLDGREEN, NORMAL, PINK, noresponse, NORMAL);
  946.  
  947.  
  948.                  printf("Thanks for using %sS%sh%so%sk%sD%si%sa%sl %s%s%s\n", 
  949.               BLINKCYAN, BOLDGREEN, BOLDBLUE, BOLDPINK, YELLOW, 
  950.                          BOLDWHITE, BOLDRED, PINK, BOLDBLUE, VERSION, NORMAL);
  951.  
  952.  
  953.                  break;
  954.              }
  955.        }
  956.  
  957.           memset(phonenum, 0, sizeof(phonenum));
  958.         } 
  959.   } 
  960.  
  961.   fclose(logfile);
  962.  
  963. /* --------------------------------------- */
  964.  
  965. void confdial(char *confile)
  966. {
  967.   time_t tm;           /* Where we our calendar time is stored        */
  968.   FILE   *logfile;     /* For the log file                        */
  969.   FILE   *confd;       /* For the config file                           */
  970.   char   date[32];     /* Contain time scanning started/stopped         */
  971.   char   pnum1[20];    /* Phone # without the '-'s and what not.        */
  972.   char   phonenum[20]; /* This will include the ATDT etc.               */
  973.     
  974.   if ((logfile = fopen(LogFile, "a")) == NULL) {
  975.      perror("fopen");
  976.      exit(ERROR);
  977.   }
  978.  
  979.   fprintf(logfile, "\n----------------------\n\n");
  980.   fflush(logfile);
  981.  
  982.   if ((confd = fopen(confile, "r")) == NULL) {
  983.      perror("fopen");
  984.      exit(ERROR);
  985.   }
  986.  
  987.   memset(buf,  0, sizeof(buf));
  988.   memset(date, 0, sizeof(date));
  989.  
  990.   tm = time(NULL);
  991.   sprintf(date, "%s", ctime(&tm));
  992.  
  993.   printf("Reading phone numbers from \"%s%s%s\".\n", PINK, confile, NORMAL);
  994.  
  995.   printf("\nNOTE: There is no checking of the phone number for -c or -s\n"
  996.          "to allow you to enter odd strings such as \"5551234,,,5#\".\n\n");
  997.  
  998.   fprintf(logfile, "Started at/on: %s\n"
  999.            "Reading phone numbers from config file \"%s\".\n", 
  1000.                 date, confile);
  1001.  
  1002.   fflush(logfile);
  1003.   memset(date, 0, sizeof(date));
  1004.  
  1005.   if (daemon == 1) putchar('\n'); /* Just to make it look nicer */
  1006.  
  1007.   printf("Using a %s%d%s second connection %stimeout%s.\n", 
  1008.       BOLDCYAN, TIMEOUT, NORMAL, BOLDWHITE, NORMAL);
  1009.  
  1010.   
  1011.   memset(pnum1, 0, sizeof(pnum1));
  1012.   memset(phonenum, 0, sizeof(phonenum));
  1013.  
  1014.   while (!feof(confd)) {
  1015.      if ((fgets(pnum, 512, confd)) == NULL) {
  1016.     perror("fgets");
  1017.     exit(ERROR);
  1018.      }
  1019.  
  1020.      if (pnum[0] == '\n')
  1021.     continue;
  1022.  
  1023.      if ((strstr(pnum, "#")) != NULL) {
  1024.  
  1025.     if (pnum[0] == '#')
  1026.         continue; 
  1027.     else {
  1028.        /* Well either there are some spaces, or a */
  1029.        /* number before the comment               */
  1030.  
  1031.        char *p, *p1;
  1032.        char temp[20];
  1033.  
  1034.        memset(temp, 0, sizeof(temp));
  1035.  
  1036.        p = pnum, p1 = temp;
  1037.  
  1038.         while(*p == '\t' || *p == ' ')
  1039.           *p += 1;
  1040.  
  1041.           if (*p == '#') /* Just some space and a comment */
  1042.           continue;
  1043.               else { /* Okay it's a number */
  1044.              while(*p != '\t' || *p != ' '  || \
  1045.                        *p != '\n' || *p != '\0' || *p != '#')
  1046.             *p1++ = *p++;
  1047.  
  1048.             sprintf(pnum, "%s", temp);    
  1049.                 
  1050.           }
  1051.     }
  1052.  
  1053.      }
  1054.  
  1055.      fprintf(logfile, "Dialing %s\n", pnum);
  1056.      fflush(logfile);
  1057.  
  1058.      strip(pnum, pnum1);
  1059.  
  1060.      printf("Dialing %s%s%s\n", BOLDCYAN, pnum, NORMAL);
  1061.  
  1062.      sprintf(phonenum, "ATDT%s\r", pnum1);
  1063.  
  1064.      numbytes = write(fd, phonenum, strlen(phonenum));
  1065.      check_for_error(LogFile, fd, numbytes, "write");
  1066.  
  1067.      memset(buf, 0, sizeof(buf));
  1068.  
  1069.      alarm(TIMEOUT); /* How long to wait for timeout   */
  1070.  
  1071.      sig = 0;
  1072.      connected = 1; /* 
  1073.                  * Easier to set it to 1 and then set it
  1074.              * to 0 if it's not than vice versa
  1075.              */
  1076.  
  1077.      do {
  1078.         numbytes = read(fd, buf, 511);
  1079.     if (sig == 1) break;
  1080.      } while ((strstr(buf, "CONNECT")) == NULL);
  1081.  
  1082.      alarm(0); /* Stop the timing. */
  1083.  
  1084.      /* Compare the string with "CONNECT" */
  1085.      if (connected == 1) {
  1086. #ifdef BEEP
  1087.         putchar('\a');
  1088. #endif
  1089.  
  1090.         fprintf(logfile, "*** CONNECT *** to %s", pnum);
  1091.     printf("%s*** %sCONNECT %s%s*** %s to %s%s%s\n", 
  1092.             BOLDWHITE, BOLDCYAN, NORMAL, BOLDWHITE, NORMAL, 
  1093.             PINK, pnum, NORMAL);
  1094.  
  1095.         /* Send a string to the carrier and check for response */
  1096.         if (send && listen) { /* send poke string and listen for reply */
  1097.  
  1098.            if (write(fd, sendstring, sizeof(sendstring)) == ERROR) {
  1099.           perror("write");
  1100.           close(fd);
  1101.           exit(ERROR);
  1102.            }
  1103.  
  1104.            response = 1; /* Sighandler will set this to 0 if it times out */
  1105.  
  1106.            printf("response from carrier (after sending string): ");
  1107.            fprintf(logfile, "response from carrier (after sending string): ");
  1108.            fflush(stdout), fflush(logfile);
  1109.  
  1110.        if (read(fd, buf, sizeof(buf)) == ERROR) {
  1111.           perror("read");
  1112.               printf("continuing anyway...\n");
  1113.        }
  1114.  
  1115.            if (response == 1) {
  1116.               printf("%s\n", buf);
  1117.               fprintf(logfile, "%s\n", buf);
  1118.            } else {
  1119.           printf("timed out while waiting for response\n");
  1120.           fprintf(logfile, "timed out while waiting for response\n");
  1121.            }
  1122.         } else { /* listen = 1, send = 0 */
  1123.  
  1124.            response = 1; /* The sighandler will set this to 0 if it */
  1125.                          /* times out                               */
  1126.  
  1127.            printf("response from carrier: ");
  1128.            fprintf(logfile, "response from carrier: ");
  1129.  
  1130.        if (read(fd, buf, sizeof(buf)) == ERROR) {
  1131.           perror("read");
  1132.               printf("continuing anyway...\n");
  1133.        }
  1134.  
  1135.            if (response == 1) {
  1136.               printf("%s\n", buf);
  1137.               fprintf(logfile, "%s\n", buf);
  1138.            } else {
  1139.               printf("timed out while waiting for response\n");
  1140.               fprintf(logfile, "timed out while waiting for response\n");
  1141.            }
  1142.         }
  1143.      }
  1144.  
  1145.      memset(buf, 0, sizeof(buf));
  1146.  
  1147.      hangup();
  1148.   }
  1149.  
  1150. #ifdef BEEP
  1151.   putchar('\a');
  1152. #endif
  1153.  
  1154.   memset(date, 0, sizeof(date));
  1155.   tm = time(NULL);
  1156.   sprintf(date, "%s", ctime(&tm));
  1157.  
  1158.   fprintf(logfile, "Finished dialing at/on: %s", date);
  1159.   fflush(logfile);                
  1160.  
  1161.   printf("Finished dialing!!\n"); 
  1162.  
  1163.   printf("Thanks for using %sS%sh%so%sk%sD%si%sa%sl %s%s%s.\n", 
  1164.        BLINKCYAN, BOLDGREEN, BOLDBLUE, BOLDPINK, YELLOW, BOLDWHITE, 
  1165.           BOLDRED, PINK, BOLDBLUE, VERSION, NORMAL);
  1166.  
  1167.   fclose(logfile);
  1168.   return;
  1169.  
  1170. /* -------------------------------------- */
  1171.  
  1172. void inputdial()
  1173. {
  1174.   time_t tm;           /* Where we our calendar time is stored        */
  1175.   FILE   *logfile;     /* For the log file                        */
  1176.   char   date[32];     /* Contain time scanning started/stopped         */
  1177.   char   phonenum[20]; /* This will include the ATDT etc.               */
  1178.     
  1179.   /* Get location of numbers: local numbers or long distance numbers */
  1180.  
  1181.   LorD:
  1182.     printf("Scanning..\n(%sL%s)ocal, Long (%sD%s)istance: ", 
  1183.           PINK, NORMAL, PINK, NORMAL);
  1184.  
  1185.   while(1) {
  1186.      LocalOrLong = getchar();
  1187.  
  1188.      if (!isprint(LocalOrLong)) continue;
  1189.      if ((toupper(LocalOrLong) != 'L') && (toupper(LocalOrLong) != 'D')) {
  1190.         printf("%sInvalid%s option '%s%c%s'. Enter '%sL%s' or '%sD%s'.\n\n",
  1191.                BOLDRED, NORMAL, BOLDCYAN, LocalOrLong, NORMAL, YELLOW,
  1192.                NORMAL, YELLOW, NORMAL);
  1193.         goto LorD; /* Reprint message. */ 
  1194.     } else break;
  1195.   }
  1196.  
  1197.   if ((logfile = fopen(LogFile, "a")) == NULL) {
  1198.      perror("fopen");
  1199.      exit(ERROR);
  1200.   }
  1201.  
  1202.   fprintf(logfile, "\n----------------------\n\n");
  1203.   fflush(logfile);
  1204.  
  1205.   memset(buf,  0, sizeof(buf));
  1206.   memset(date, 0, sizeof(date));
  1207.  
  1208.   tm = time(NULL);
  1209.   sprintf(date, "%s", ctime(&tm));
  1210.  
  1211.   fprintf(logfile, "Started at/on: %s\n", date);
  1212.   fprintf(logfile, "Reading phone numbers stdin.\n");
  1213.  
  1214.   fflush(logfile);
  1215.   memset(date, 0, sizeof(date));
  1216.  
  1217.   if (daemon == 1) putchar('\n'); /* Just to make it look nicer */
  1218.  
  1219.   printf("Using a %s%d%s second connection %stimeout%s.\n", 
  1220.       BOLDCYAN, TIMEOUT, NORMAL, BOLDWHITE, NORMAL);
  1221.  
  1222.   memset(phonenum, 0, sizeof(phonenum));
  1223.  
  1224.   printf("When finished, enter \"%s.%s\" as the number.\n", 
  1225.          BOLDWHITE, NORMAL); 
  1226.  
  1227.   printf("\nNOTE: There is no checking of the phone number for -c or -s\n"
  1228.          "to allow you to enter odd strings such as \"5551234,,,5#\".\n\n");
  1229.  
  1230.   signal(SIGINT,  sighandler);
  1231.   signal(SIGTERM, sighandler);
  1232.  
  1233.   while (1) {
  1234.      if (toupper(LocalOrLong) == 'L') { /* Use local phone numbers */
  1235.  
  1236.         printf("Enter phone number (i.e. 555-5555): ");
  1237.         scanf("%3d%*c%4d", &First3Digits, &ScanMin);
  1238.  
  1239.         /* First3Digits and ScanMin will both be 0 if "." is entered */
  1240.         if (First3Digits == 0 && ScanMin == 0) goto finished; 
  1241.  
  1242.         sprintf(pnum, "%.3d%.4d", First3Digits, ScanMin);
  1243.  
  1244.         fprintf(logfile, "Dialing %.3d-%.4d\n", First3Digits, ScanMin);
  1245.         fflush(logfile);
  1246.  
  1247.      } else { /* LocalOrLong == 'D', use long distance phone numbers */
  1248.  
  1249.         printf("Enter phone number (i.e. 555-555-5555): ");
  1250.         scanf("%3d%*c%3d%*c%4d", &First3Digits, &Last3Digits, &ScanMin);
  1251.  
  1252.         /* First3Digits and ScanMin will both be 0 if "." is entered */
  1253.         if (First3Digits == 0 && ScanMin == 0 && Last3Digits == 0) 
  1254.            goto finished; 
  1255.  
  1256.         sprintf(pnum, "1%.3d%.3d%.4d", First3Digits, Last3Digits, ScanMin);
  1257.  
  1258.         fprintf(logfile, "Dialing %.3d-%.3d-%.4d\n", 
  1259.                 First3Digits, Last3Digits, ScanMin);
  1260.         fflush(logfile);
  1261.      }
  1262.  
  1263.      sprintf(phonenum, "ATDT%s\r", pnum);
  1264.      numbytes = write(fd, phonenum, strlen(phonenum));
  1265.      check_for_error(LogFile, fd, numbytes, "write");
  1266.  
  1267.      memset(buf, 0, sizeof(buf));
  1268.  
  1269.      alarm(TIMEOUT); /* How long to wait for timeout   */
  1270.  
  1271.      sig = 0;
  1272.      connected = 1; /* 
  1273.                   * Easier to set it to 1 and then set it
  1274.              * to 0 if it's not than vice versa
  1275.              */
  1276.  
  1277.      do {
  1278.         numbytes = read(fd, buf, 511);
  1279.     if (sig == 1) break;
  1280.      } while ((strstr(buf, "CONNECT")) == NULL);
  1281.  
  1282.      alarm(0); /* Stop the timing. */
  1283.  
  1284.      /* Compare the string with "CONNECT" */
  1285.      if (connected == 1) {
  1286. #ifdef BEEP
  1287.         putchar('\a');
  1288. #endif
  1289.  
  1290.         fprintf(logfile, "*** CONNECT *** to %s", pnum);
  1291.     printf("%s*** %sCONNECT %s%s*** %s to %s%s%s\n", 
  1292.                BOLDWHITE, BOLDCYAN, NORMAL, BOLDWHITE, NORMAL, 
  1293.                PINK, pnum, NORMAL);
  1294.  
  1295.         if (send && listen) { /* send poke string and listen for reply */
  1296.  
  1297.            if (write(fd, sendstring, sizeof(sendstring)) == ERROR) {
  1298.           perror("write");
  1299.           close(fd);
  1300.           exit(ERROR);
  1301.            }
  1302.  
  1303.            response = 1; /* The sighandler returns 0 when it times out */
  1304.  
  1305.            printf("response from carrier (after sending string): ");
  1306.            fprintf(logfile, "response from carrier (after sending string): ");
  1307.            fflush(stdout), fflush(logfile);
  1308.  
  1309.        if (read(fd, buf, sizeof(buf)) == ERROR) {
  1310.           perror("read");
  1311.               printf("continuing anyway...\n");
  1312.        }
  1313.  
  1314.            if (response == 1) {
  1315.                printf("%s\n", buf);
  1316.                fprintf(logfile, "%s\n", buf);
  1317.            } else {
  1318.                printf("timed out while waiting for response\n");
  1319.                fprintf(logfile, "timed out while waiting for response\n");
  1320.            }
  1321.         } else { /* listen = 1, send = 0 */
  1322.  
  1323.            response = 1; /* The sighandler will set this to 0 if it */
  1324.                          /* times out                               */
  1325.  
  1326.            printf("response from carrier: ");
  1327.            fprintf(logfile, "response from carrier: ");
  1328.            fflush(stdout), fflush(logfile);
  1329.  
  1330.        if (read(fd, buf, sizeof(buf)) == ERROR) {
  1331.           perror("read");
  1332.               printf("continuing anyway...\n");
  1333.        }
  1334.  
  1335.            if (response == 1) {
  1336.               printf("%s\n", buf);
  1337.               fprintf(logfile, "%s\n", buf);
  1338.            } else {
  1339.               printf("timed out while waiting for response\n");
  1340.               fprintf(logfile, "timed out while waiting for response\n");
  1341.            }
  1342.         }
  1343.      }
  1344.  
  1345.      memset(buf, 0, sizeof(buf));
  1346.  
  1347.      hangup();
  1348.   }
  1349.  
  1350.  
  1351.   finished:
  1352.      memset(date, 0, sizeof(date));
  1353.      tm = time(NULL);
  1354.      sprintf(date, "%s", ctime(&tm));
  1355.  
  1356.      fprintf(logfile, "User ended dialing at/on: %s", date);
  1357.      fflush(logfile);                
  1358.  
  1359.      printf("Okay I hope you enjoyed it!\n"); 
  1360.  
  1361.      printf("Thanks for using %sS%sh%so%sk%sD%si%sa%sl %s%s%s.\n", 
  1362.            BLINKCYAN, BOLDGREEN, BOLDBLUE, BOLDPINK, YELLOW, BOLDWHITE, 
  1363.             BOLDRED, PINK, BOLDBLUE, VERSION, NORMAL);
  1364.  
  1365.      fclose(logfile);
  1366.      return;
  1367.  
  1368. /* -------------------------------------- */
  1369.  
  1370. void hangup()
  1371. {
  1372.   FILE *logfile;
  1373.  
  1374.   if ((logfile = fopen(LogFile, "a")) == NULL) {
  1375.      perror("fopen");
  1376.      exit(ERROR);
  1377.   }
  1378.     
  1379.   /* 
  1380.    * The reason we write "ATH" to a nonconnected host is that
  1381.    * this is fine. But when it's connected... +++ is sent as
  1382.    * the login name, and ATH as the password (not a good thing
  1383.    * to be logged on a remote host anyway. ;)
  1384.    * If it is connected we will take the less effecient method
  1385.    * of closing and reopening the fd to hang up
  1386.    */
  1387.  
  1388.   if (connected != 1) {
  1389.      numbytes = write(fd, "+++\r", 4);
  1390.      check_for_error(LogFile, fd, numbytes, "write");
  1391.  
  1392.      usleep(500000);
  1393.      memset(buf, 0, sizeof(buf));
  1394.  
  1395.      numbytes = write(fd, "ATH0\r", 5);
  1396.      check_for_error(LogFile, fd, numbytes, "write");
  1397.  
  1398.  
  1399.      /* 
  1400.       * We're using SIGALRM, and sleep() uses sig alarm
  1401.       * and usleep() doesn't.                 
  1402.       */
  1403.  
  1404.      usleep(1000000); 
  1405.  
  1406.      numbytes = read(fd, buf, sizeof(buf));
  1407.      check_for_error(LogFile, fd, numbytes, "read");
  1408.      usleep(2000000);
  1409.  
  1410.      if (noOK != 1) noOK = checkok(LogFile, fd, buf, "hanging up modem");
  1411.      else {
  1412.         /* There was an error getting an "OK" from the modem */
  1413.         fclose(logfile);
  1414.         close(fd), exit(ERROR);
  1415.      }
  1416.  
  1417.      if (noOK == 1) {
  1418.         /* There was an error getting an "OK" from the modem */
  1419.         fclose(logfile);
  1420.         close(fd), exit(ERROR);
  1421.      }
  1422.  
  1423.   } else {
  1424.       if (close(fd) == ERROR) {
  1425.      perror("close");
  1426.      exit(ERROR);
  1427.       }
  1428.  
  1429.       open_port();
  1430.       connected = 0;
  1431.   }
  1432.  
  1433.   memset(buf, 0, sizeof(buf));
  1434.   fclose(logfile);
  1435. }
  1436.  
  1437. /* -------------------------------------- */
  1438.  
  1439. /* The reason I have two different sighandler functions, rather than */
  1440. /* just basing off the signal number, is simplicity.                 */
  1441.  
  1442.  
  1443. void sighandler(int signum)
  1444. {
  1445.   FILE   *logfile;
  1446.   char   date[32]; /* Where the date for the ending time is stored. */
  1447.   time_t tm;       /* Where calendar time is stored.                */
  1448.  
  1449.   memset(date, 0, sizeof(date));
  1450.  
  1451.   /* Just exit on one of these signals. */
  1452.   signal(SIGINT,  stopnow);
  1453.   signal(SIGTERM, stopnow);
  1454.  
  1455.   tm = time(NULL);
  1456.   sprintf(date, "%s", ctime(&tm));
  1457.  
  1458.   if ((logfile = fopen(LogFile, "a")) == NULL) {
  1459.      perror("fopen");
  1460.      exit(ERROR);
  1461.   }
  1462.  
  1463.   printf("%sReceived signal to quit%s:\nClosing up modem, logging, and exitting.\n", 
  1464.       BOLDRED, NORMAL);
  1465.   fprintf(logfile, "\nReceived signal to quit. Aborting.\n");
  1466.   fflush(logfile);
  1467.  
  1468.   if (conf == 1) {
  1469.      fprintf(logfile, "Last number dialed was %s", pnum);
  1470.      close(fd);
  1471.      fclose(logfile);
  1472.      exit(ERROR);
  1473.   }
  1474.  
  1475.   if (toupper(LocalOrLong) == 'L') { /* Use local phone numbers */
  1476.      if (rand != 1 || conf != 1) {
  1477.     fprintf(logfile, "Last number dialed was %.3d-%.4d.\n", 
  1478.                 First3Digits, ScanMin);
  1479.  
  1480.         printf("Last number dialed was %s%.3d-%.4d%s.\n", 
  1481.             BOLDCYAN, First3Digits, ScanMin, NORMAL);
  1482.      }
  1483.  
  1484.      fprintf(logfile, "Results:\n");
  1485.      fprintf(logfile, "\t# of successful connects: %d\n", connect);
  1486.      fprintf(logfile, "\t# of busy numbers: %d\n", busy);
  1487.      fprintf(logfile, "\t# of no responses (timed out): %d\n", noresponse);
  1488.  
  1489.   } else { /* if LocalOrLong == 'D' */
  1490.        if (rand != 1 || conf != 1) {
  1491.        fprintf(logfile, "Last number dialed was 1-%.3d-%.3d-%.4d.\n", 
  1492.             First3Digits, Last3Digits, ScanMin);
  1493.  
  1494.       printf("Last number dialed was %s1-%.3d-%.3d-%.4d%s.\n", 
  1495.           BOLDCYAN, First3Digits, Last3Digits, ScanMin, NORMAL);
  1496.        }
  1497.  
  1498.        fprintf(logfile, "Results:\n");
  1499.        fprintf(logfile, "\t# of successful connects: %d\n", connect);
  1500.        fprintf(logfile, "\t# of busy numbers: %d\n", busy);
  1501.        fprintf(logfile, "\t# of no responses (timed out): %d\n", noresponse);
  1502.  
  1503.   }
  1504.  
  1505.   /* Print statistics. */
  1506.   printf("%sResults%s:\n", BOLDRED, NORMAL);
  1507.  
  1508.   printf("\t# of %ssuccessful connects%s: %s%d%s\n", 
  1509.           BOLDCYAN, NORMAL, PINK, connect, NORMAL);
  1510.  
  1511.   printf("\t# of %sno responses (timed out)%s: %s%d%s\n", 
  1512.           YELLOW, NORMAL, PINK, busy, NORMAL);
  1513.  
  1514.   printf("\t# of %sno responses (timed out)%s: %s%d%s\n", 
  1515.           BOLDGREEN, NORMAL, PINK, noresponse, NORMAL);
  1516.  
  1517.  
  1518.   fprintf(logfile, "Aborted at: %s", date);
  1519.   fflush(logfile);
  1520.  
  1521.   noshow = 1; /* So we don't get 'Opening modem for dialing' because  */
  1522.               /* we use open_port() for both hanging up and dialing.  */
  1523.  
  1524.   hangup(); 
  1525.  
  1526.   close(fd);
  1527.   fclose(logfile);
  1528.  
  1529.   exit(0);
  1530. }
  1531.  
  1532. /* -------------------------------------- */
  1533.  
  1534. void sighandler1(int signum)
  1535. {  
  1536.    signal(SIGALRM, sighandler1);
  1537.  
  1538.    sig = 1;
  1539.    response = 0;
  1540.    connected = 0;
  1541. }
  1542.  
  1543. /* -------------------------------------- */
  1544.  
  1545. void menu(int signum)
  1546. {
  1547.   char ch;
  1548.  
  1549.   signal(SIGINT, sighandler);
  1550.   signal(SIGTERM, sighandler);
  1551.  
  1552.   printf("\n\n1. Hang up modem and skip to next number\n");
  1553.   printf("2. Hang up modem and exit\n\n");
  1554.   printf("Enter 1 or 2: ");
  1555.  
  1556.   while (1) {
  1557.      fflush(stdout);
  1558.   
  1559.      ch = getchar();
  1560.  
  1561.      if (ch == '1') {
  1562.  
  1563.         alarm(0); /* Stop the timeout timer. */
  1564.  
  1565.         /* Just act like the number timed out. sighandler1 is */
  1566.         /* the sig handler called when a number times out.    */
  1567.         sighandler1(0); 
  1568.  
  1569.         /* Reset signal handlers. */
  1570.         signal(SIGINT,  menu);
  1571.         signal(SIGTERM, menu);
  1572.  
  1573.         break;
  1574.      } else if (ch == '2') {
  1575.         /* Sig handler used to exit. So we will just call this. */
  1576.         sighandler(0); 
  1577.  
  1578.      } else
  1579.     if (isprint(ch)) printf("Invalid option.\nEnter 1 or 2: ");
  1580.   }
  1581. }
  1582.  
  1583. void stopnow(int signum)
  1584. {
  1585.   /* Exit immediately. */
  1586.   exit(ERROR);
  1587. }
  1588.